package ar.com.ford.it.fullEconomics.view.form;


import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class PartsWithoutCostStructReportForm extends ValidatorForm {
    private String section;
    private String prefix;
    private String base;
    private String suffix;
    private String partClass;
    private String dateInMin;
    private String dateInMax;
    private String dateOutMin;
    private String dateOutMax;
    private String supplierClass;
    private String supplierCode;
    private String commodity;
    private String vehicleLine;
    private Collection parts;

    /**
     * Resetea el formulario
     * 
     */
    public void resetForm() {
        this.section = "";
        this.supplierCode = null;
        this.prefix = null;
        this.base = null;
        this.suffix = null;
        this.dateInMin = null;
        this.dateInMax = null;
        this.dateOutMin = null;
        this.dateOutMax = null;
        this.parts = null;
        this.partClass = "-1";
        this.supplierClass = "-1";
    }


    /**
     * Genera las validaciones necesarias.
     */
    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);

        if ("find".equalsIgnoreCase(this.getSection())) {
            if (!StringUtils.isBlank(this.getDateInMin())) {
                if (StringUtils.isBlank(this.getDateInMax())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "part.error.invalidRange",
                                            resources
                                                    .getMessage("part.label.tableHeader.fechaIn")));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateInMin()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateInMin()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateInMin()));
                }
            }
            if (!StringUtils.isBlank(this.getDateInMax())) {
                if (StringUtils.isBlank(this.getDateInMin())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "part.error.invalidRange",
                                            resources
                                                    .getMessage("part.label.tableHeader.fechaIn")));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateInMax()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateInMax()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateInMax()));
                }
            }
            if (!StringUtils.isBlank(this.getDateOutMin())) {
                if (StringUtils.isBlank(this.getDateOutMax())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "part.error.invalidRange",
                                            resources
                                                    .getMessage("part.label.tableHeader.fechaOut")));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateOutMin()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateOutMin()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateOutMin()));
                }
            }
            if (!StringUtils.isBlank(this.getDateOutMax())) {
                if (StringUtils.isBlank(this.getDateOutMin())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "part.error.invalidRange",
                                            resources
                                                    .getMessage("part.label.tableHeader.fechaOut")));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateOutMax()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateOutMax()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateOutMax()));
                }
            }
        }
        return ae;
    }

    public Collection getParts() {
        return parts;
    }

    public void setParts(Collection parts) {
        this.parts = parts;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getSupplierClass() {
        return supplierClass;
    }

    public void setSupplierClass(String supplierClass) {
        this.supplierClass = supplierClass;
    }

    public String getSupplierCode() {
        return supplierCode;
    }

    public void setSupplierCode(String supplierCode) {
        this.supplierCode = supplierCode.toUpperCase();
    }

    public String getDateInMax() {
        return dateInMax;
    }

    public void setDateInMax(String dateInMax) {
        this.dateInMax = dateInMax;
    }

    public String getDateInMin() {
        return dateInMin;
    }

    public void setDateInMin(String dateInMin) {
        this.dateInMin = dateInMin;
    }

    public String getDateOutMax() {
        return dateOutMax;
    }

    public void setDateOutMax(String dateOutMax) {
        this.dateOutMax = dateOutMax;
    }

    public String getDateOutMin() {
        return dateOutMin;
    }

    public void setDateOutMin(String dateOutMin) {
        this.dateOutMin = dateOutMin;
    }

    public String getCommodity() {
        return commodity;
    }


    public void setCommodity(String commodity) {
        this.commodity = commodity;
    }


    public String getVehicleLine() {
        return vehicleLine;
    }


    public void setVehicleLine(String vehicleLine) {
        this.vehicleLine = vehicleLine;
    }


    public String getBase() {
        return base;
    }


    public void setBase(String base) {
        this.base = base.toUpperCase();
    }


    public String getPartClass() {
        return partClass;
    }


    public void setPartClass(String partClass) {
        this.partClass = partClass;
    }


    public String getPrefix() {
        return prefix;
    }


    public void setPrefix(String prefix) {
        this.prefix = prefix.toUpperCase();
    }


    public String getSuffix() {
        return suffix;
    }


    public void setSuffix(String suffix) {
        this.suffix = suffix.toUpperCase();
    }

}
